package com.raven.craft.code.generator.common.file;

import com.raven.craft.code.generator.common.file.metadata.FileMetadata;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.ToString;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

@Getter
@ToString
public class ExcelFile implements File {

    private final FileMetadata metadata;
    private final Workbook workbook;

    public ExcelFile(FileMetadata metadata, Workbook workbook) {
        this.metadata = metadata;
        this.workbook = workbook;
    }

    @SneakyThrows
    @Override
    public void output() {
        Path path = Paths.get(getMetadata().getFullFilePath());
        OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
        getWorkbook().write(outputStream);
    }
}
